package com.example.demo.util;

import com.example.demo.Entity.SysUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.ui.Model;

import javax.jws.WebParam;

public class UserUtil {
    public static String userLogin(SysUser user, Model model)
    {
        Subject subject= SecurityUtils.getSubject();
        UsernamePasswordToken token=new UsernamePasswordToken(user.getUsername(),user.getPassword());
        try{
            subject.login(token);
           // user.setRoleList();
         //   subject.hasRole("admin");
            model.addAttribute("username",user.getUsername());
            return "redirect:/index";
        }catch (UnknownAccountException e)
        {
            model.addAttribute("msg","用户不存在");
            return "login";
        }
        catch (IncorrectCredentialsException
         e){
            model.addAttribute("msg","密码错误");
            return "login";
        }
    }
    public static String encrptPassword(String password,String salt){
        return new Md5Hash(password,salt,3).toString();
    }
}
